package com.qqhru.oj.model.vo;

import cn.hutool.json.JSONUtil;
import com.qqhru.oj.judge.codesandbox.model.JudgeInfo;
import com.qqhru.oj.model.entity.QuestionSubmit;
import lombok.Data;
import org.springframework.beans.BeanUtils;
import java.io.Serializable;
import java.util.Date;

/**
 * 题目封装类，专门返回给前端的
 * 题目表中哪些字段不应该返回给前端：
 */
@Data
public class QuestionSubmitVO implements Serializable {
    /**
     * id
     */
    private Long id;

    /**
     * 编程语言
     */
    private String language;

    /**
     * 用户代码
     */
    private String code;

    /**
     * 判断信息（json对象）
     */
    private JudgeInfo judgeInfo;

    /**
     * 判题状态（0 - 待判题、1 - 判题中、2 - 成功、3 - 失败）
     */
    private Integer status;

    /**
     * 题目 id
     */
    private Long questionId;

    /**
     * 创建用户 id
     */
    private Long userId;

    /**
     * 创建时间
     */
    private Date createTime;

    /**
     * 更新时间
     */
    private Date updateTime;

    /*提交用户信息：谁提交的，具体用户名是谁？*/
    private UserVO userVO;

    /*对应题目信息*/
    private QuestionVO questionVO;

    /**
     * 包装类转对象 将questionSubmitVO转换成questionSubmit
     *
     * @param questionSubmitVO
     * @return
     */
    public static QuestionSubmit voToObj(QuestionSubmitVO questionSubmitVO) {
        if (questionSubmitVO == null) {
            return null;
        }
        QuestionSubmit questionSubmit = new QuestionSubmit();
        BeanUtils.copyProperties(questionSubmitVO, questionSubmit);
        JudgeInfo JudgeInfoObj = questionSubmitVO.getJudgeInfo();
        if (JudgeInfoObj != null) {
            /*JSONUtil.toJsonStr(voJudgeConfig)把对象转换成字符串*/
            questionSubmit.setJudgeInfo(JSONUtil.toJsonStr(JudgeInfoObj));
        }
        return questionSubmit;
    }

    /**
     * 对象转包装类 将QuestionSubmit转成QuestionSubmitVO
     *
     * @param questionSubmit
     * @return
     */
    public static QuestionSubmitVO objToVo(QuestionSubmit questionSubmit) {
        if (questionSubmit == null) {
            return null;
        }
        QuestionSubmitVO questionSubmitVO = new QuestionSubmitVO();
        BeanUtils.copyProperties(questionSubmit, questionSubmitVO);
        String JudgeInfoStr = questionSubmit.getJudgeInfo();
        questionSubmitVO.setJudgeInfo(JSONUtil.toBean(JudgeInfoStr, JudgeInfo.class));
        return questionSubmitVO;
    }

    private static final long serialVersionUID = 1L;
}
